package com.snap.awesomeserial.utils;

import android.app.ActivityManager;
import android.content.ComponentName;

import com.snap.awesomeserial.MoleApplication;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

import static android.content.Context.ACTIVITY_SERVICE;

public class AutoUtil {


    /**
     * 获取UUID
     *
     * @return
     */
    public static String getUUID32() {
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        return uuid;
    }

    /**
     * 获取文件名
     */
    public static String getFileName() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        String date = sdf.format(new Date());
        return "p" + date;
    }

    /**
     * 获取日期
     */
    public static String getDaySecond(Date date) {
        //2019-09-02 15:00
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(date);
    }

    /**
     * 获取日期
     */
    public static String getHour(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(time);
    }

    /**
     * 获取时分秒
     *
     * @param endTime
     * @param beginTime
     * @return
     */
    public static String getChronometer(long endTime, long beginTime) {
        long runTime = (endTime - beginTime) / 1000;
        if (runTime < 0) runTime = 0;
        long hour = runTime / 3600;
        long min = runTime % 3600 / 60;
        long second = runTime % 60;
        return String.format(Locale.CHINA, "%02d:%02d:%02d", hour, min, second);
    }

    private String getFileNameTv() {
        String fileName;
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        fileName = String.valueOf(cal.get(Calendar.YEAR)) + month;//当前年
        return fileName;
    }

    /**
     * 得到栈顶的activity
     * 路径+类名
     * 需要在清单文件中添加权限 <uses-permission android:name = “android.permission.GET_TASKS”/>
     */
    public static String getTopActivity() {

        ActivityManager manager = (ActivityManager) MoleApplication.get().getSystemService(ACTIVITY_SERVICE);

        List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
        ComponentName componentName;
        if (runningTaskInfos != null) {
            componentName = runningTaskInfos.get(0).topActivity;
            String result = componentName.getClassName();
            return result;
        } else {
            return null;
        }
    }

}
